
import axios from 'axios'
import qs from 'qs'
import {message} from '../common/util'
import store from '../vuex'
import router from '../router'
import vm from '../main'

//网站绝对路径配置
export let contextPath = ''
// 微信重定向地址
export let weiXinRedirect = ''

if(window.location.href.indexOf('www.matrix98.com') > 0){
  /*
  真实服务器
  * */
  contextPath = 'https://www.matrix98.com/jhxx_ruiyi/'
  switch (sessionStorage.getItem('index')) {
    case 0:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8bcd537696343e18&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2Fmember%3Findex%3D0&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 1:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6e53bec9862ed008&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2Fmember%3Findex%3D1&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 2:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8492a220294ac518&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2Fmember%3Findex%3D2&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    default:break
  }
}else{
  /*
  本地测试服务器
  * */
  contextPath = 'http://192.168.1.140:9900/jhxx_ruiyi/' //改成想要链接的本地测试服务器路径
  //本地微信重定向路径（啊明）
  /* switch (sessionStorage.getItem('index')) {
    case 0:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe3e017b28e4180b9&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_ming-office.html%3findex%3d0&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 1:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6e53bec9862ed008&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_ming-office.html%3findex%3d1&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 2:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8492a220294ac518&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_ming-office.html%3findex%3d2&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    default:break
  } */
  //本地微信重定向路径（啊辉）
  /*switch (sessionStorage.getItem('index')) {
    case 0:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe3e017b28e4180b9&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_Jason-office.html%3findex%3d0&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 1:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6e53bec9862ed008&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_Jason-office.html%3findex%3d1&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 2:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8492a220294ac518&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_Jason-office.html%3findex%3d2&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    default:break
  }*/
  //本地微信重定向路径（啊塱）
  /*switch (sessionStorage.getItem('index')) {
    case 0:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe3e017b28e4180b9&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_lang-office.html%3findex%3d0&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 1:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6e53bec9862ed008&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_lang-office.html%3findex%3d1&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    case 2:weiXinRedirect = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8492a220294ac518&redirect_uri=https%3A%2F%2Fwww.matrix98.com%2FweixinTest%2Fmember_lang-office.html%3findex%3d2&response_type=code&scope=snsapi_userinfo&state=188#wechat_redirect';break
    default:break
  }*/
  /*
  使用mock假数据
  * */
  contextPath = ''
  weiXinRedirect  = 'http://192.168.1.173:8084'//改成自己的本地vue运行路径
}

// axios 配置
axios.defaults.timeout = 20000;
axios.defaults.baseURL = contextPath + 'api/member/';//调用数据接口

// http request 拦截器，通过这个，我们就可以把Cookie传到后台
axios.interceptors.request.use(
  config => {
    const userId = sessionStorage.getItem("userid") //获取userId
    //config.data = JSON.stringify(config.data);
    //后台接收的参数
    config.data = qs.stringify(config.data)
    if(config.data != '') config.data += '&'
    config.data += 'accessTypeCode=1&'
    config.data += 'USER_ID='+userId + '&'
    config.data += 'dbIndex='+ sessionStorage.getItem('index')
    //console.log(config.data)
    config.headers = {
      'Content-Type':'application/x-www-form-urlencoded' //设置跨域头部
    };
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

// http response 拦截器
axios.interceptors.response.use(
  response => {
//response.data.errCode是我接口返回的值，如果值为2，说明Cookie丢失，然后跳转到重定向地址，这里根据大家自己的情况来设定
    if(response.data.status == 'timeout') {
      console.log('interceptors redirect!')
      message({message:'系统超时，请重新进入系统，或关闭页面后重新进入系统',type:'error'})
      window.location.href = weiXinRedirect
      /*router.push({
        path: '/login',
        query: {redirect: router.currentRoute.fullPath}  //从哪个页面跳转
      })*/
    }
    return response
  },
  error => {
    return Promise.reject(error.response.data)
  })

//网络异常或服务器宕机时候执行的操作
function axiosTimeout() {
  store.commit('updateLoadingStatus',{isLoading:false})
  vm.$vux.confirm.show({
    title:'网络请求异常',
    content:'网络异常，请尝试以下操作，或关闭页面后重新进入系统',
    confirmText:'刷新当前页面',
    cancelText:'重新进入系统',
    onCancel () {
      window.location.href = weiXinRedirect//这里要跳到重定向地址
    },
    onConfirm () {
      router.go(0)
    }
  })
}


export const getWeiXinConfig = params => {
  let reqParams = {//传到后台的数据
    url:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getJsSignature', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getWeiXinConfig axios err:' + err);axiosTimeout()})
  })
}

export const weiXinAuth = params => {
  let reqParams = {//传到后台的数据
    code:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('weiXinAuth', reqParams).then(res => resolve(res.data)).catch((err) => {reject('weiXinAuth axios err:' + err);axiosTimeout()})
  })
}

export const bindPhone = params => {
  let reqParams = {//传到后台的数据
    PHONE:params.phone,
    checkCode:params.checkCode,
    OPENID:params.openId
  }
  return new Promise((resolve, reject)=>{
    axios.post('bindPhone', reqParams).then(res => resolve(res.data)).catch((err) => {reject('bindPhone axios err:' + err);axiosTimeout()})
  })
}

export const getUserInfo = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getUserInfo', params).then(res => resolve(res.data)).catch((err) => {reject('getUserInfo axios err:' + err);axiosTimeout()})
  })
}

export const sendCheckCode = params => {
  let reqParams = {//传到后台的数据
    phone:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('sendCheckCode', reqParams).then(res => resolve(res.data)).catch((err) => {reject('sendCheckCode axios err:' + err);axiosTimeout()})
  })
}

export const getNoticeIdList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getNoticeIdList', params).then(res => resolve(res.data)).catch((err) => {reject('getNoticeIdList axios err:' + err);axiosTimeout()})
  })
}

export const getNoticeByIds = params => {
  let reqParams = {//传到后台的数据
    NOTICE_IDS:JSON.stringify(params)
  }
  return new Promise((resolve, reject)=>{
    axios.post('getNoticeByIds', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getNoticeByIds axios err:' + err);axiosTimeout()})
  })
}

export const getNoticeDetail = params => {
  let reqParams = {//传到后台的数据
    NOTICE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getNoticeDetail', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getNoticeDetail axios err:' + err);axiosTimeout()})
  })
}

/*export const changePassword = params => {
  let reqParams = {//传到后台的数据
    phone: params.phone,
    checkCode: params.checkCode,
    password: params.password,
    password2: params.password2
  }
  return new Promise((resolve, reject)=>{
    axios.post('changePassword', reqParams).then(res => resolve(res.data)).catch((err) => {reject('changePassword axios err:' + err);axiosTimeout()})
  })
}*/

/*export const getAuthority = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getAuthority', params).then(res => resolve(res.data)).catch((err) => {reject('getAuthority axios err:' + err);axiosTimeout()})
  })
}*/

export const getBusinessHours = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getBusinessHours', params).then(res => resolve(res.data)).catch((err) => {reject('getBusinessHours axios err:' + err);axiosTimeout()})
  })
}

export const getSubscribeList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getSubscribeList', params).then(res => resolve(res.data)).catch((err) => {reject('getSubscribeList axios err:' + err);axiosTimeout()})
  })
}

export const getRoomType = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getRoomType', params).then(res => resolve(res.data)).catch((err) => {reject('getRoomType axios err:' + err);axiosTimeout()})
  })
}

export const checkRegistered = params => {
  let reqParams = {//传到后台的数据
    PHONE:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('checkRegistered', reqParams).then(res => resolve(res.data)).catch((err) => {reject('checkRegistered axios err:' + err);axiosTimeout()})
  })
}

export const submitSubscribe = params => {
  let reqParams = {//传到后台的数据
    subscribe: params.subscribe,//当为普通预订时候值为normal，当为节日/派对预订时候值为HOLIDAYPARTY_ID的值
    //后台应先判断该HOLIDAYPARTY_ID的STATE状态是否能够进行预订，状态不为“预约中”，即返回不成功的信息
    subscribeTime: params.subscribeTime,
    choosenRoomType: params.choosenRoomType,
    PHONE: params.phone,
    name: params.name,
    consumptionNum: params.consumptionNum,
    remark: params.remark
  }
  return new Promise((resolve, reject)=>{
    axios.post('submitSubscribe', reqParams).then(res => resolve(res.data)).catch((err) => {reject('submitSubscribe axios err:' + err);axiosTimeout()})
  })
}

export const getHolidayPartyById = params => {
  let reqParams = {//传到后台的数据
    HOLIDAYPARTY_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getHolidayPartyById', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getHolidayPartyById axios err:' + err);axiosTimeout()})
  })
}

export const getHolidayPartyList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getHolidayPartyList', params).then(res => resolve(res.data)).catch((err) => {reject('getHolidayPartyList axios err:' + err);axiosTimeout()})
  })
}

export const getSubscribeById = params => {
  let reqParams = {//传到后台的数据
    SUBSCRIBE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getSubscribeById', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getSubscribeById axios err:' + err);axiosTimeout()})
  })
}

export const getDepositAbout = params => {
  let reqParams = {//传到后台的数据
    ROOMPLATFORMTYPE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getDepositAbout', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getDepositAbout axios err:' + err);axiosTimeout()})
  })
}

export const getDepositTerm = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getDepositTerm', params).then(res => resolve(res.data)).catch((err) => {reject('getDepositTerm axios err:' + err);axiosTimeout()})
  })
}

export const cancelSubscribe = params => {
  let reqParams = {//传到后台的数据
    SUBSCRIBE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('cancelSubscribe', reqParams).then(res => resolve(res.data)).catch((err) => {reject('cancelSubscribe axios err:' + err);axiosTimeout()})
  })
}

export const getKeepList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getKeepList', params).then(res => resolve(res.data)).catch((err) => {reject('getKeepList axios err:' + err);axiosTimeout()})
  })
}

export const getKeepDetail = params => {
  let reqParams = {//传到后台的数据
    KEEPWINE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getKeepDetail', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getKeepDetail axios err:' + err);axiosTimeout()})
  })
}

export const getChoosenTable = params => {
  let reqParams = {//传到后台的数据
    ROOMPLATFORM_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getChoosenTable', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getChoosenTable axios err:' + err);axiosTimeout()})
  })
}

export const getPersonKeepList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getPersonKeepList', params).then(res => resolve(res.data)).catch((err) => {reject('getPersonKeepList axios err:' + err);axiosTimeout()})
  })
}

export const withDrawKeepWine = params => {
  let reqParams = {//传到后台的数据
    selectedItems:JSON.stringify(params.selectedItems),
    ROOMPLATFORM_ID:params.tableId
  }
  return new Promise((resolve, reject)=>{
    axios.post('withDrawKeepWine', reqParams).then(res => resolve(res.data)).catch((err) => {reject('withDrawKeepWine axios err:' + err);axiosTimeout()})
  })
}

export const getCallList = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getCallList', params).then(res => resolve(res.data)).catch((err) => {reject('getCallList axios err:' + err);axiosTimeout()})
  })
}

export const callAgain = params => {
  let reqParams = {//传到后台的数据
    CALLSERVICE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('callAgain', reqParams).then(res => resolve(res.data)).catch((err) => {reject('callAgain axios err:' + err);axiosTimeout()})
  })
}

export const getLowerType = params => {
  let reqParams = {//传到后台的数据
    CALLSERVICETYPE_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getLowerType', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getLowerType axios err:' + err);axiosTimeout()})
  })
}

export const submitCallService = params => {
  let reqParams = {//传到后台的数据
    REMARK:params.remark,
    CALLSERVICETYPE_ID:params.type,
    ROOMPLATFORM_ID:params.tableId
  }
  return new Promise((resolve, reject)=>{
    axios.post('submitCallService', reqParams).then(res => resolve(res.data)).catch((err) => {reject('submitCallService axios err:' + err);axiosTimeout()})
  })
}

export const getCardCount = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getCardCount', params).then(res => resolve(res.data)).catch((err) => {reject('getCardCount axios err:' + err);axiosTimeout()})
  })
}

export const getPayCodeValue = params => {
  return new Promise((resolve, reject)=>{
    axios.post('getPayCodeValue', params).then(res => resolve(res.data)).catch((err) => {reject('getPayCodeValue axios err:' + err);axiosTimeout()})
  })
}

export const getCardList = params => {
  let reqParams = {//传到后台的数据
    type:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getCardList', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getCardList axios err:' + err);axiosTimeout()})
  })
}

export const getCardDetail = params => {
  let reqParams = {//传到后台的数据
    MEMBERCARDUSER_ID:params
  }
  return new Promise((resolve, reject)=>{
    axios.post('getCardDetail', reqParams).then(res => resolve(res.data)).catch((err) => {reject('getCardDetail axios err:' + err);axiosTimeout()})
  })
}

